<?php
    /**
     * @copyright 2014
     * @author Thanh NV 
     * @Version 1.0
     * @Description : model salary from database to Excel
     */

	if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Mmod_payroll extends MYAD_Model{
		protected $table_name = 'payroll';
        function __construct(){
			parent::__construct();                         
		}
    /**
     * @author Thanh NV        
     * @function countAll 
     * @param int $bat a number to be multiplied 
     * @param int $baz a number to be multiplied 
     * @return int the product of the two parameters 
     * @This function caculating total record  
     */             
      
        function countAll($fullname=NULL) {  
            $month=date('m',$this->getMonthForSalary());
            $year=date('Y',$this->getMonthForSalary()); 
//            $month=1;
//            $year=2014;
            $this->db->select('*');
            $this->db->from('payroll');
            $this->db->join('m_employee','m_employee.EmployeeId=payroll.EmployeeId');
            $this->db->where(array('payroll.Month'=>$month,'payroll.Year'=>$year));            
            $this->db->like(array('EmployeeName' => $fullname));
            $query = $this->db->get();           
            return $query->num_rows();
        } 
     /**
        * function getEmployeeSearch
        * get all record with conditions
     */         
        function getEmployeeSearch($limit=NULL,$offset=NULL,$fullname=NULL)
        {        
            $month=date('m',$this->getMonthForSalary());
            $year=date('Y',$this->getMonthForSalary()); 
//            $month=1;
//            $year=2014;
            $this->db->select('*');
            $this->db->from('payroll');
            $this->db->order_by("PayrollId", "desc"); 
            $this->db->where(array('payroll.Month'=>$month,'payroll.Year'=>$year));            
            $this->db->join('m_employee','m_employee.EmployeeId=payroll.EmployeeId');            
            if(!is_null($limit)) $this->db->limit($limit, $offset);
            if(!empty($fullname) && !is_null($fullname))$this->db->like(array('EmployeeName' => $fullname));
            $query = $this->db->get();
            return $query->result_array();
        }
       /**
        * function getEmployeeSearch
        * This function caculating total salary on a month
     */          

        function getTotalSalaryOnMonth($fullname=NULL,$month=NULL) {
            //$this->db->select('*');
            $month=date('m',$this->getMonthForSalary());
            $year=date('Y',$this->getMonthForSalary());
            $this->db->select_sum('NetSalary');
            $this->db->from('payroll');
            $this->db->where(array('payroll.Month'=>$month,'payroll.Year'=>$year));            
            $this->db->join('m_employee','m_employee.EmployeeId=payroll.EmployeeId');
            $this->db->like(array('EmployeeName' => $fullname));
            $query = $this->db->get();             
            return $query->result_array();
        }
       /**
        * function getEmployeeID
        * This fuction get Employee with EmployeeId
     */         
      
        function getEmployeeID($employeeId=NULL,$month=NULL,$year=NULL) {
           
            $this->db->select('*');
            $this->db->from('payroll');
            $this->db->join('m_employee','m_employee.EmployeeId=payroll.EmployeeId');
            $this->db->join('m_position','m_employee.PositionId=m_position.PositionId');
            if(!is_null($month)) {
                    $this->db->where(array('m_employee.EmployeeId'=> $employeeId,'payroll.Month'=>$month,'payroll.Year'=>$year));            
            }else{
                
                    $this->db->where('m_employee.EmployeeId', $employeeId);            
            }        
            $query = $this->db->get();           
            return $query->result_array();
        }
       /**
        * function getAll
        * get All table 
        */        
        //       
        function getAll(){
            $this->db->select('*');
            $this->db->from('payroll');
            $this->db->order_by("PayrollId", "desc"); 
            $query = $this->db->get();
            return $query->result_array();
            }        
        }      
        




 